package com.travel.server.manager.impl.command.order.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.order.data.QuerySaleTargetListRequest;
import com.travel.server.manager.dto.request.project.TimeRequest;
import com.travel.server.manager.dto.response.order.data.QuerySaleTargetListResponse;
import com.travel.server.manager.dto.response.order.data.QuerySaleTargetListResponse.TargetItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.model.dto.data.OrderSaleTargetDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/7 16:32
 * @Desc:
 */
@Component
@Slf4j
public class QuerySaleTargetListCommand extends AbstractCommand<QuerySaleTargetListRequest, QuerySaleTargetListResponse> {

    @Autowired
    private OrderDal orderDal;

    @Autowired
    private ProjectDal projectDal;

    @Override
    protected void checkInput(QuerySaleTargetListRequest request) {
        checkPageInput(request);
        checkTimeInput(request);
    }

    @Override
    public QuerySaleTargetListResponse handle(QuerySaleTargetListRequest request) {
        checkInput(request);

        QuerySaleTargetListResponse response = new QuerySaleTargetListResponse();
        response.setTargetList(makeTargetList(request));
        response.setTotalNum(projectDal.selectUsefulTotal());
        return response;
    }

    public List<TargetItem> makeTargetList(TimeRequest request){
        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());
        List<OrderSaleTargetDto> list = orderDal.selectSaleTarget(startTime, endTime, request.getOffset(), request.getLimit());
        List<TargetItem> targetList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(list)){
            for(OrderSaleTargetDto dto : list){
                TargetItem item = new TargetItem();
                item.setOrder(dto.getOrder() + "");
                item.setRefund(dto.getRefund() + "");
                item.setOrderCharge(dto.getChargeName());
                item.setProject(dto.getProjectName());
                item.setTotal(dto.getTotal() + "");
                item.setTransfer(dto.getTransfer() + "");
                targetList.add(item);
            }
        }
        return targetList;
    }
}
